package GUI;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import java.awt.Font;

public class Bank extends Menu{
	private JTable table;
	public static String scenarioName;
	public static String scenarioId;
	
	public Bank(ArrayList<Object> msg)
	{
	
		initComponents(msg);	
	}
	
	private void initComponents(ArrayList<Object> str)  {
		
		
		Menu.btnRun.setEnabled(false); //setting buttons
		Menu.btnDeleteScen.setEnabled(false);
		Menu.btnUpdatescenario.setEnabled(false);
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(110, 148, 611, 278);
		add(scrollPane);

		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		 
	
        for (int i=1; i<=(str.size()-4);i=i+4){
        Vector<Object> row = creatrow(str,i);
        data.add(row);
        }
	
        Vector<String> headers = new Vector<String>();
        headers.add("Scenario ID");
        headers.add("Scenario Name");
        headers.add("Version");
        headers.add("Project");
        final JTable table = new JTable( data, headers );
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		int col = table.getSelectedColumn();   
                int row = table.getSelectedRow();
                boolean is=table.isColumnSelected(0);
                if (!is){
        			
        			JOptionPane.showMessageDialog(null, "Please select scenario ID only","Error",JOptionPane.ERROR_MESSAGE);

        		}
                else
                {
                	Menu.btnRun.setEnabled(true);
                	Menu.btnDeleteScen.setEnabled(true);
            		Menu.btnUpdatescenario.setEnabled(true);
                	
            		scenarioId=((String)table.getValueAt(row, col));
            		
            		Object o2=((String)table.getValueAt(row, col+1));
            		scenarioName=(String)o2;
            		
                }
        		
        	}
        });
       // table.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        JTableHeader header = table.getTableHeader();
        header.setDefaultRenderer(centerRenderer);
        for (int i=0;i<table.getColumnCount();i++)
        {
        	table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );
        	
        }
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(new Dimension(900,150));
        scrollPane.setViewportView(table);

		
        
        
        
        
        
        
	}
	
	
	
	
	
	private Vector<Object> creatrow(ArrayList<Object>str, int i) {
		 Vector<Object> row = new Vector<Object>();
     row.add( ""+str.get(i));
     row.add( ""+str.get(i+1));
     row.add( ""+str.get(i+2));
     row.add( ""+str.get(i+3));

		return row;
	}
}
